package org.skydiveapplibrary.data;

import java.util.Calendar;
import java.util.Date;

public class RigReminder
{
	public enum DueStatus
	{
		PastDue,
		DueSoon,
		NotDue
	};
	
	private static final int NOTICE_PERIOD_DAYS = 7;
	
	private int id;
	private String name;
	private Date lastCompletedDate;
	private TimeUnit intervalUnit;
	private int interval;

	public int getId()
	{
		return id;
	}
	public void setId(int id)
	{
		this.id = id;
	}
	public String getName()
	{
		return name;
	}
	public void setName(String name)
	{
		this.name = name;
	}
	public Date getLastCompletedDate()
	{
		return lastCompletedDate;
	}
	public void setLastCompletedDate(Date lastCompletedDate)
	{
		this.lastCompletedDate = lastCompletedDate;
	}
	
	public Date getDueDate()
	{
		return getDueDate(
				getInterval(),
				getIntervalUnit(),
				getLastCompletedDate());
	}
	
	public DueStatus getDueStatus() 
	{
		return getDueStatus(getDueDate());
	}
	
	public int getInterval()
	{
		return interval;
	}
	public void setInterval(int interval)
	{
		this.interval = interval;
	}
	public TimeUnit getIntervalUnit()
	{
		return intervalUnit;
	}
	public void setIntervalUnit(TimeUnit intervalUnit)
	{
		this.intervalUnit = intervalUnit;
	}
	
	public static Date getDueDate(
			int interval,
			TimeUnit intervalUnit,
			Date lastCompletedDate)
	{
		// update due date
		Calendar cal = Calendar.getInstance();
		cal.setTime(lastCompletedDate);
		cal.add(intervalUnit.getCalendarField(), interval);
		return cal.getTime();
	}
	
	public static DueStatus getDueStatus(Date dueDate)
	{
		// get notice date
		Calendar cal = Calendar.getInstance();
		cal.setTime(dueDate);
		cal.add(Calendar.DATE, NOTICE_PERIOD_DAYS * -1);
		Date noticeDate = cal.getTime();
		
		// get now
		Date now = new Date();
		
		// compare notice date to now
		if (now.compareTo(noticeDate) < 0)
		{
			return DueStatus.NotDue;
		}
		else if (now.compareTo(noticeDate) >= 0 &&
				now.compareTo(dueDate) < 0)
		{
			return DueStatus.DueSoon;
		}
		else
		{
			return DueStatus.PastDue;
		}
	}
}
